/**
  ******************************************************************************
  * @file           : produp.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __PRODUP_H
#define __PRODUP_H

#include <stdint.h>
#include "ethernet.h"

#define PROD_DBFILE_HEADER                   0x44414546u  /* FEAD */
#define PROD_DATABASE_HEADER                 0x4D455449u  /* ITEM */
#define PROD_DATABASE_M                      'M'
#define PROD_DATABASE_POSI                   0x49534F50u  /* POSI */

#define PRODUP_ETHFB_CTRL_QUE_LENGTH         5
#define PRODUP_ETHFB_CTRL_QUE_ITEMSIZE       sizeof(Ethernet_FeedbackCtrlStru)

#define PRODUP_ETHFB_DATA_QUE_LENGTH         5
#define PRODUP_ETHFB_DATA_QUE_ITEMSIZE       sizeof(Ethernet_FeedbackDataStru)

#define PROD_DATABASE_QUE_LENGTH             5
#define PROD_DATABASE_QUE_ITEMSIZE           sizeof(Prod_DataBaseStru)

/* 数据库文件头 */
typedef struct _Prod_DBFileHeaderStru
{
  uint32_t header;
  uint32_t besent;    /* 已发送的数量 */
  uint32_t allitm;    /* 总保存的数量 */
  uint32_t xorchk;    /* 对除header外的成员的异或校验值 */
}Prod_DBFileHeaderStru;

/* size 80 */
typedef struct _Prod_DataBaseStru
{
  uint32_t header;
  uint32_t serial;
  uint16_t year;
  uint8_t  month;
  uint8_t  day;
  uint8_t  hour;
  uint8_t  minute;
  uint8_t  second;
  uint8_t  state;
  float    belt_speed_mms;
  uint32_t posi;             /* 用于结构体填空 插入定位值 */
  double   flowrate_kgh;
  uint64_t interval_acc_0d1g;
  uint64_t total_acc_0d1g;
  uint64_t year_acc_0d1g;
  uint64_t month_acc_0d1g;
  uint64_t day_acc_0d1g;
  uint64_t hour_acc_0d1g;
}Prod_DataBaseStru;

extern uint8_t sd_detect_mount(void);
extern uint8_t proddb_save(Prod_DataBaseStru *produp, uint8_t *isrtdata);
extern uint8_t proddb_check(Prod_DataBaseStru *produp, uint8_t *ishsdata);
extern uint8_t proddb_rcvback(void);

extern uint8_t produp_data_loop(Ethernet_SendQueStru *tbuf, Prod_DataBaseStru *pdb);

extern volatile uint8_t produp_recv_ok;

#endif /* __PRODUP_H */
